package fr.inserm.umr915.vcfserver.vcf;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collections;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class AbstractVcfFilterServlet extends HttpServlet
	{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
		{
		resp.setContentType("text/plain");
		PrintWriter out=resp.getWriter();
		out.println(req.getMethod());
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if(!isMultipart )
			{
			throw new ServletException("Not a multipart query");
			}
		ServletFileUpload upload = new ServletFileUpload();
			
		// Parse the request
		try {
			
			FileItemIterator iter = upload.getItemIterator(req);
			while (iter.hasNext())
				{
			    FileItemStream item = iter.next();
			    out.println("##name:"+item.getName()+" field:"+item.getFieldName());
			    InputStream in = item.openStream();
			    if (item.isFormField())
			    	{
			    	out.print("form field");
			    	}
			    else
			    	{
			    	 int c;
		            while((c=in.read())!=-1)
		                {
		                out.write((char)Character.toUpperCase(c));
		                }
			    	}
			    out.flush();
			    in.close();
				}
		} catch (FileUploadException e)
			{
			throw new IOException(e);
			}
		out.flush();
		out.close();
		}
	}
